package com.ebt.m.customer.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.customer.util.DisplayUtil;

public class CustomerInfoUnitEditTextForProfile4Address extends LinearLayout {

    private static final int DEFAULT_TEXT_VIEW_TEXT_COLOR = 0xff999999;
    private static final int DEFAULT_EDIT_TEXT_TEXT_COLOR = 0xff333333;
    private static final int DEFAULT_EDIT_TEXT_HINT_COLOR = 0xff999999;

    private static final int DEFAULT_TEXT_VIEW_TEXT_SIZE_SP = 16;
    private static final int DEFAULT_EDIT_TEXT_TEXT_SIZE_SP = 16;

    private Context mContext;

    private String mTextViewText;
    private String mEditTextHint;
    private String mEditTextText;

    private float mEditTextTextSize = -1;
    private float mTextViewTextSize = -1;

    private int mEditTextTextColor = DEFAULT_EDIT_TEXT_TEXT_COLOR;
    private int mTextViewTextColor = DEFAULT_TEXT_VIEW_TEXT_COLOR;
    private int mEditTextHintColor = DEFAULT_EDIT_TEXT_HINT_COLOR;

    private boolean mShowTextView = true;
    private boolean mShowRightImageView = false;
    private boolean mShowDividerView = true;

    private int mImageViewSrcId = 0;
    private int mImageViewRightSrcId = 0;

    private ImageView customer_info_unit_imageview_left;
    private ImageView customer_info_unit_imageview_right;
    private TextView customer_info_unit_textview;
    private TextView customer_info_unit_edittext;
    private View divider;

    public CustomerInfoUnitEditTextForProfile4Address(Context context) {
        this(context, null);
    }

    public CustomerInfoUnitEditTextForProfile4Address(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomerInfoUnitEditTextForProfile4Address(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
        View contentView = inflate(context, R.layout.view_customer_info_unit_edit_text_for_profile_address, this);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.customerInfoUnit);

        final int count = a.getIndexCount();
        for (int i = 0; i < count; ++i) {
            int attr = a.getIndex(i);
            switch (attr) {
                case R.styleable.customerInfoUnit_customerInfoUnitText:
                    mTextViewText = a.getString(attr);
                    break;
                case R.styleable.customerInfoUnit_customerInfoUnitHint:
                    mEditTextHint = a.getString(attr);
                    break;
                case R.styleable.customerInfoUnit_customerInfoUnitShowText:
                    mShowTextView = a.getBoolean(attr, true);
                    break;
                case R.styleable.customerInfoUnit_customerInfoUnitShowRightImage:
                    mShowRightImageView = a.getBoolean(attr, false);
                    break;
                case R.styleable.customerInfoUnit_customerInfoUnitShowDivider:
                    mShowDividerView = a.getBoolean(attr, true);
                    break;
                case R.styleable.customerInfoUnit_customerInfoUnitEditTextColor:
                    mEditTextTextColor = a.getColor(attr, DEFAULT_EDIT_TEXT_TEXT_COLOR);
                    break;
                case R.styleable.customerInfoUnit_customerInfoUnitEditTextSize:
                    mEditTextTextSize = a.getDimensionPixelSize(attr, DisplayUtil.sp2px(mContext, DEFAULT_EDIT_TEXT_TEXT_SIZE_SP));
                    break;
                case R.styleable.customerInfoUnit_customerInfoUnitTextColor:
                    mTextViewTextColor = a.getColor(attr, DEFAULT_TEXT_VIEW_TEXT_COLOR);
                    break;
                case R.styleable.customerInfoUnit_customerInfoUnitTextSize:
                    mTextViewTextSize = a.getDimensionPixelSize(attr, DisplayUtil.sp2px(mContext, DEFAULT_TEXT_VIEW_TEXT_SIZE_SP));
                    break;
                case R.styleable.customerInfoUnit_customerInfoUnitHintColor:
                    mEditTextHintColor = a.getColor(attr, DEFAULT_EDIT_TEXT_HINT_COLOR);
                    break;
                case R.styleable.customerInfoUnit_customerInfoUnitImageSrc:
                    mImageViewSrcId = a.getResourceId(attr, 0);
                    break;
                case R.styleable.customerInfoUnit_customerInfoUnitRightImageSrc:
                    mImageViewRightSrcId = a.getResourceId(attr, 0);
                    break;
            }
        }
        a.recycle();

        customer_info_unit_imageview_left = (ImageView) contentView.findViewById(R.id.customer_info_unit_imageview_left);
        customer_info_unit_imageview_right = (ImageView) contentView.findViewById(R.id.customer_info_unit_imageview_right);

        customer_info_unit_textview = (TextView) contentView.findViewById(R.id.customer_info_unit_textview);
        customer_info_unit_edittext = (TextView) contentView.findViewById(R.id.customer_info_unit_edittext);
        divider = contentView.findViewById(R.id.divider);

        divider.setVisibility(mShowDividerView ? View.VISIBLE : View.INVISIBLE);
        if (mImageViewSrcId == 0) {
            customer_info_unit_imageview_left.setVisibility(View.GONE);
        } else {
            customer_info_unit_imageview_left.setVisibility(View.VISIBLE);
            customer_info_unit_imageview_left.setImageDrawable(getContext().getResources().getDrawable(mImageViewSrcId));
        }
        if (!TextUtils.isEmpty(mTextViewText)) {
            customer_info_unit_textview.setText(mTextViewText);
        }
        if (!TextUtils.isEmpty(mEditTextHint)) {
            customer_info_unit_edittext.setHint(mEditTextHint);
        }
        if (!TextUtils.isEmpty(mEditTextText)) {
            customer_info_unit_edittext.setText(mEditTextText);
        }

        if (mShowTextView) {
            if (customer_info_unit_textview.getVisibility() != View.VISIBLE) {
                customer_info_unit_textview.setVisibility(View.VISIBLE);
            }
        } else {
            if (customer_info_unit_textview.getVisibility() != View.GONE) {
                customer_info_unit_textview.setVisibility(View.GONE);
            }
        }

        if (mShowRightImageView) {
            if (customer_info_unit_imageview_right.getVisibility() != View.VISIBLE) {
                customer_info_unit_imageview_right.setVisibility(View.VISIBLE);
            }
        } else {
            if (customer_info_unit_imageview_right.getVisibility() != View.GONE) {
                customer_info_unit_imageview_right.setVisibility(View.GONE);
            }
        }

        if (mImageViewRightSrcId == 0) {
            customer_info_unit_imageview_right.setVisibility(View.GONE);
        } else {
            customer_info_unit_imageview_right.setVisibility(View.VISIBLE);
            customer_info_unit_imageview_right.setImageDrawable(getContext().getResources().getDrawable(mImageViewRightSrcId));
        }

        customer_info_unit_edittext.setTextColor(mEditTextTextColor);
        customer_info_unit_textview.setTextColor(mTextViewTextColor);
        customer_info_unit_edittext.setHintTextColor(mEditTextHintColor);

        if (mEditTextTextSize >= 0) {
            customer_info_unit_edittext.setTextSize(DisplayUtil.px2sp(mContext, mEditTextTextSize));
        }
        if (mTextViewTextSize >= 0) {
            customer_info_unit_textview.setTextSize(DisplayUtil.px2sp(mContext, mTextViewTextSize));
        }

    }

    public void setTextViewText(String text) {
        mTextViewText = text;
        if (customer_info_unit_textview != null) {
            customer_info_unit_textview.setText(mTextViewText);
        }
    }

    public void setEditTextHint(String hint) {
        mEditTextHint = hint;
        if (customer_info_unit_edittext != null) {
            customer_info_unit_edittext.setHint(mEditTextHint);
        }
    }

    public void setEditTextText(String text) {
        mEditTextText = text;
        if (customer_info_unit_edittext != null) {
            customer_info_unit_edittext.setText(mEditTextText);
        }
    }

    public String getTextViewText() {
        return (String) customer_info_unit_textview.getText();
    }

    public String getEditTextHint() {
        return (String) customer_info_unit_edittext.getHint();
    }

    public String getEditTextText() {
        return customer_info_unit_edittext.getText().toString().trim();
    }

    public void setEditTextOnClickListener(OnClickListener listener, Object tag) {
        customer_info_unit_edittext.setTag(tag);
        customer_info_unit_edittext.setOnClickListener(listener);
    }

    public void setEditTextOnFocusChangeListener(OnFocusChangeListener listener, Object tag) {
        customer_info_unit_edittext.setTag(tag);
        customer_info_unit_edittext.setOnFocusChangeListener(listener);
    }

    public void setFocusable(boolean b) {
        customer_info_unit_edittext.setFocusable(b);
    }

    public TextView getEditText() {
        return customer_info_unit_edittext;
    }

    public TextView getTextView() {
        return customer_info_unit_textview;
    }

}
